<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/************Default Controller******************************/
class Default_Controller extends MY_Controller{
	public function __construct(){
		parent::__construct();		
		$this->loadScript(base_url().'js/jquery-1.10.1.min.js');
		
		//Set Language
		$this->load->helper(array('url','language'));
		$language = $this->uri->segment(1);
		if($language == 'vn'){
			$this->config->set_item('language', 'vn');
		}elseif($language == 'en'){
				$this->config->set_item('language', 'en');
			}else{
				$this->config->set_item('language', 'vn');
				
			}
		$this->lang->load('site');		
		////////////////////////////////////////////////////
	}	
	protected function state_login(){
		if(!($this->session->userdata('state_login_default')) && $this->uri->segment(3) != "login"){
			redirect('default/account/login');
		}
	}
	
}

/************Admin Controller********************************/
class Admin_Controller extends MY_Controller{
	public function __construct(){
		parent::__construct();
		$this->_layout = "templates/administrator/layout";
		$this->headMetaCharset('utf-8');
		$this->loadStylesheet(base_url().'plugins/bootstrap/css/bootstrap.css');
		$this->loadStylesheet(base_url().'plugins/bootstrap/css/bootstrap-formhelpers.min.css');
		$this->loadStylesheet(base_url().'css/style.admin.css');
		$this->loadScript(base_url().'js/jquery-1.10.1.min.js');
		$this->loadScript(base_url().'js/config.js');
		$this->loadScript(base_url().'plugins/bootstrap/js/bootstrap.min.js');
		$this->loadScript(base_url().'plugins/bootstrap/js/bootstrap-formhelpers.min.js');
		$this->loadScript(base_url().'js/jquery.tooltip.js');
		$this->loadScript(base_url().'js/jquery.cookie.min.js');
		$this->loadScript(base_url().'js/admin/jquery.sidebar.js');
		$this->load->helper('html');
		$this->state_login();
	}	
	
	public function init_pagination($uri, $total_rows, $per_page=10, $num_link=6){
		$this->load->library('pagination');
		$config['base_url'] = base_url().$uri;
		$config['total_rows'] = $total_rows; 
		//$config['uri_segment'] = $segment;
		$config['per_page'] = $per_page; 
		$config['num_links'] = $num_link; 
		$config['use_page_numbers'] = TRUE;
		$config['page_query_string'] = TRUE;
		$config['query_string_segment'] = 'page';
		$config['full_tag_open'] = '<ul>';
		$config['full_tag_close'] = '</ul>';
		$config['first_tag_open'] = $config['last_tag_open']= $config['next_tag_open']= $config['prev_tag_open'] = $config['num_tag_open'] = '<li>';
        $config['first_tag_close'] = $config['last_tag_close']= $config['next_tag_close']= $config['prev_tag_close'] = $config['num_tag_close'] = '</li>';
         
        $config['cur_tag_open'] = "<li class='active'><span>";
        $config['cur_tag_close'] = "</span></li>";
		$config['next_link'] = 'Next';
		$config['prev_link'] = 'Previous';
		$this->pagination->initialize($config);
		return $config;
	}
	
	//Kiem tra Trang thai dang nhap
	protected function state_login(){
		if((!($this->session->userdata('state_login_admin')) || $this->session->userdata('p') != md5(md5("ADMIN"))) && $this->uri->segment(2) != "login"){
			redirect('administrator/login');
		}
	}
	
}







































/*************************************************************************************************************/
class MY_Controller extends CI_Controller{
	public $data = array();
	public $_layout;
	public function __construct(){
		parent::__construct();
		/***********/
		$this->data["arrayMetaHttpEquiv"] = array();
		$this->data["arrayMetaCharset"] = array();
		$this->data["arrayMetaName"] = array();
		$this->data["arrayScripts"] = array();
		$this->data["arrayStyles"] = array();
		$this->data["arraySidebar"] = array();
		$this->data["arrayBreadcrumb"] = array();
		$this->data["titlePage"] = "Website";
		/**********/
		
		date_default_timezone_set("Asia/Saigon");
		$this->load->library('session');
	}
	
	/**
	 * Tải script vào view
	 * @urlscript   Đường dẫn của script
	 */
	public function loadScript($urlscript){
		array_push($this->data["arrayScripts"],$urlscript);
	}
	
	/**
	 * Tải css vào view
	 * @urlstyle   Đường dẫn của css
	 */
	public function loadStylesheet($urlstyle){
		array_push($this->data["arrayStyles"],$urlstyle);
	}
	
	/**
	 * Tải dữ liệu vào view
	 * @key   Tên biến truy xuất
	 * @value Dữ liệu được truyền
	 */
	public function loadData($key, $value){
		$this->data[$key] = $value;
	}
	
	/**
	 * Tải view
	 * @view   Đường dẫn của view
	 */
	public function loadView($view){
		if(isset($this->_layout)){
			$this->data["content_layout"] = $view;
			$this->load->view($this->_layout, $this->data);
		}else{
			echo "Không có layout";
		}
	}
	
	/**
	 * Đặt tiêu đề trang
	 * @title   Tên tiêu đề
	 */
	 public function headTitle($title){
	 	$this->data["titlePage"] = $title;
	 }
	 
	 /**
	 * Đặt meta http equiv cho trang
	 * @http_equiv   Giá trị của http-equiv
	 * @content		 Giá trị của content
	 */
	 public function headMetaHttpEquiv($http_equiv, $content){
	 	array_push($this->data["arrayMetaHttpEquiv"],array("http_equiv" => $http_equiv, "content" => $content));
	 }
	 
	 /**
	 * Đặt meta charset cho trang
	 * @charset   Giá trị của charset
	 */
	 public function headMetaCharset($charset){
	 	array_push($this->data["arrayMetaCharset"],$charset);
	 }
	 /*load side bar*/
	 public function loadSidebar($name, $dataSidebar = null){
	 	array_push($this->data["arraySidebar"],array("name" => $name, "dataSidebar" => $dataSidebar));
	 }
	 
	 /*load Breadbrumb*/
	 public function loadBreadcrumb($name, $url = null){
	 	array_push($this->data["arrayBreadcrumb"],array("name" => $name, "url" => $url));
	 }
	 
	 
	 /**
	 * Đặt meta name cho trang
	 * @name         Giá trị của name
	 * @content		 Giá trị của content
	 */
	 public function headMetaName($name, $content){
	 	array_push($this->data["arrayMetaName"],array("name" => $name, "content" => $content));
	 }
	
	/**
	* Xử lý tải lên hình ảnh
	* @upload_field    Tên của input upload
	* @upload_path     Đường dẫn tải lên
	*/
	 public function upload_image_process($upload_field, $upload_path) {
		$result = array();
		
		// Upload file
		$this->load->config('upload.php');
		$upload_config = $this->config->item('upload');
		$upload_config["upload_path"] = $upload_path;
		
		$this->load->library('upload', $upload_config);
		$this->upload->initialize($upload_config);
		
		if (!$this->upload->do_upload($upload_field))
		{
			// Upload bị lỗi
			$result['error_upload'] = $this->upload->display_errors();
			$result['image_full_url'] = '';
		}
		else
		{
			// Upload file successfull
			$result['upload_data'] = $this->upload->data();
			
			if (isset($result['upload_data']['is_image']) && $result['upload_data']['is_image'] == true) {
				// File vừa tải lên là file ảnh hợp lệ
				$result['image_full_url'] = $upload_config['upload_path'] . $result['upload_data']['file_name'];
			}
			else {
				// File vừa tải lên không phải là file ảnh hợp lệ
				$result['image_full_url'] = '';
			}
		}
		
		return $result;
		
	}
	
	/**
	* Hàm loại bỏ dấu và một số ký tự đặc biệt
	* @cs      Đoạn text muốn loại bỏ dấu
	*/
	public function stripUnicode($cs){
	
		$marTViet=array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă",
				"ằ","ắ","ặ","ẳ","ẵ","è","é","ẹ","ẻ","ẽ","ê","ề"
				,"ế","ệ","ể","ễ",
				"ì","í","ị","ỉ","ĩ",
				"ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
				,"ờ","ớ","ợ","ở","ỡ",
				"ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
				"ỳ","ý","ỵ","ỷ","ỹ",
				"đ",
				"À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
				,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
				"È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
				"Ì","Í","Ị","Ỉ","Ĩ",
				"Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
				,"Ờ","Ớ","Ợ","Ở","Ỡ",
				"Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
				"Ỳ","Ý","Ỵ","Ỷ","Ỹ",
				"Đ");
	
		$marKoDau=array("a","a","a","a","a","a","a","a","a","a","a"
				,"a","a","a","a","a","a",
				"e","e","e","e","e","e","e","e","e","e","e",
				"i","i","i","i","i",
				"o","o","o","o","o","o","o","o","o","o","o","o"
				,"o","o","o","o","o",
				"u","u","u","u","u","u","u","u","u","u","u",
				"y","y","y","y","y",
				"d",
				"A","A","A","A","A","A","A","A","A","A","A","A"
				,"A","A","A","A","A",
				"E","E","E","E","E","E","E","E","E","E","E",
				"I","I","I","I","I",
				"O","O","O","O","O","O","O","O","O","O","O","O"
				,"O","O","O","O","O",
				"U","U","U","U","U","U","U","U","U","U","U",
				"Y","Y","Y","Y","Y",
				"D");
		$chuhoa=array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","V","T","U","W","X","Y","Z");
		$chuthuong=array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","v","t","u","ư","x","y","z");
		$str = str_replace($marTViet,$marKoDau,$cs);
		$str = str_replace($chuhoa,$chuthuong,$str);
		$kytudacbiet =array(" ","/","\\",",",".",":","'","----","---","--");
		$daugach = array("-","-","-","-","-","-","-","-","-","-");
		$str = str_replace($kytudacbiet,$daugach,$str);
		return $str;
	}
	
	/**
	* Hàm gửi mail
	*/
	public function sendMail($mailto, $subject, $message){
		$this->load->config('email.php');
		$config = $this->config->item('email');
		$this->load->library('email',$config);
		$this->email->initialize($config);
		$this->email->set_newline("\r\n");
				
		$this->email->from($config['smtp_user'], $config['name_user']);
		$this->email->to($mailto);    
		$this->email->subject($subject);
		$this->email->message($message);  
		$this->email->reply_to($config['smtp_user'], $config['name_user']);  
			
		if($this->email->send()){
			return true;
		}
		return false;			
	}
	
	/**
	* Hàm tạo mã ngẫu nhiên
	* @length    Độ dài của mã
	*/
	public function randomCode($length){
		$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		$length_chars = strlen($chars);
		$code = "";
		for ($i=0; $i<$length; $i++){
			$code .= $chars[rand(0, $length_chars - 1)];
		}
		return $code;
	}
}